/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.html.model;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import offset.nodes.Constants;
import offset.nodes.server.model.HashMapPathConfiguratorFinder;
import offset.nodes.server.model.NodeMatcher;
import offset.nodes.server.model.NodeTypeStartupExtension;
import offset.nodes.server.model.PathConfiguratorFinder;

/**
 *
 * @author Walter L�tgenau
 */
public class CSSFinder implements PathConfiguratorFinder {
    static CSSFinder instance;
    HashMapPathConfiguratorFinder finder;

    public static synchronized CSSFinder getInstance() throws RepositoryException {
        if (instance == null)
            instance = new CSSFinder();
        return instance;
    }

    class CSSMatcher implements NodeMatcher {

        @Override
        public boolean matches(Node node) {
            try {
                if (!node.isNodeType(Constants.TYPE_NODES_FILE))
                    return false; // should not happen
                if (!node.hasNode(Constants.JCR_CONTENT))
                    return false;
                Node content = node.getNode(Constants.JCR_CONTENT);
                if (!content.hasProperty(Constants.PROP_MIME_TYPE))
                    return false;
                if (!Constants.MIMETYPE_CSS.equals(content.getProperty(Constants.PROP_MIME_TYPE).getString()))
                    return false;
                return true;
            } catch (RepositoryException ex) {
                Logger.getLogger(CSSFinder.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }

        }

    }

    public CSSFinder() throws RepositoryException {
        this.finder = new HashMapPathConfiguratorFinder(Constants.TYPE_NODES_FILE, new CSSMatcher());
    }

    @Override
    public Node[] findPathConfigurators(String path) {
        return finder.findPathConfigurators(path);
    }
}
